package it.univaq.mancoosi.injectors.systemconfiguration.managers;

import it.univaq.mancoosi.injectors.systemconfiguration.managers.ubuntu.UbuntuEnvironmentManager;
import it.univaq.mancoosi.mancoosimm.LibraryCache;
import it.univaq.mancoosi.mancoosimm.MancoosiFactory;
import it.univaq.mancoosi.mancoosimm.MenuEntry;
import it.univaq.mancoosi.mancoosimm.SharedLibrary;

import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;

public abstract class LibraryCacheManager {
	protected static LibraryCacheManager INSTANCE;
	protected MancoosiFactory factory; 
	protected LibraryCache libraryCache;
	protected HashMap<String, SharedLibrary> sharedLibraryMap; 

	protected LibraryCacheManager(){
		this.factory = MancoosiFactory.eINSTANCE;
		libraryCache = factory.createLibraryCache();
		this.sharedLibraryMap = new HashMap<String, SharedLibrary>();
	}

	public static LibraryCacheManager getInstance() {
		return INSTANCE;
	}
	
	public LibraryCache getLibraryCache() {
		return libraryCache;
	}
	
	public void syncSharedLibraryMap () {
		Iterator<SharedLibrary> iterator  = null;
		if(UbuntuEnvironmentManager.getInstance().getEnvironment().getLibraryCache() != null){
			iterator = UbuntuEnvironmentManager.getInstance().getEnvironment().getLibraryCache().getSharedLibraries().iterator();
		}
		if(iterator != null){
			while (iterator.hasNext()) {
				SharedLibrary curr = iterator.next();
				this.sharedLibraryMap.put(curr.getFile().getLocation(), curr);
			}
		}
	}

	public abstract void createLibraryCacheFromSystem();
	
	public abstract Boolean existsSharedLibrary(String sharedLibraryPath);

	public abstract void synchronize() throws IOException;
}
